import { Util } from '../../utils/util.js';

export class PermissionUnit {
    constructor({ code = '', name = '' } = {}) {
        this.code = code;
        this._name = name;
    }

    static empty() {
        return new PermissionUnit({ code: '', name: '' });
    }

    static clone(data) {
        return new PermissionUnit({
            code: data.code,
            name: data._name
        });
    }

    // 获取翻译后的名称，如果没有翻译则返回原始
    get name() {
        return this._name;
    }

    static fromJson(json) {
        return new PermissionUnit({
            code: Util.jsonToString(json.permissionCode),
            name: Util.jsonToString(json.name)
        });
    }

    toJson() {
        return {
            permissionCode: this.code,
            name: this._name
        };
    }
}
